package servlet;

import dao.AttendanceDao;
import vo.Attendance;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/one_page_attendance_detail")
public class one_page_attendance_detail extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("utf-8");

        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();

         String attendance_time;
         String teacherId;
         String index = null;
         teacherId = request.getParameter("teacherId");
         attendance_time = request.getParameter("attendance_time");

        int currentIndex, count, size = 10;
        index = request.getParameter("index");
        if(index == null) index = "1";
        currentIndex = Integer.parseInt(index);
        try{
            AttendanceDao attendanceDao = new AttendanceDao();
            List<Attendance> attendances = new ArrayList<>();
            attendances = attendanceDao.getAttendanceByTeacherId(teacherId, attendance_time);
            count = attendances.size();
            List<Attendance> attendances1 = attendanceDao.getAttendanceDetailOnePage(attendances, currentIndex, size);
            int sumIndex = count % size == 0 ? count / size : count / size + 1;
            session.setAttribute("attendances", attendances1);
            session.setAttribute("attendance_time", attendance_time);
            session.setAttribute("teacherId", teacherId);
            session.setAttribute("sumIndex", sumIndex);
            response.sendRedirect("teacher/attendance_detail.jsp");

        }catch (Exception e){
            out.print(e);
        }



    }


}
